import xmw
from google.appengine.ext import db
import logging

class Thread_db(xmw.Thread):
    sub_count=db.IntegerProperty(default=0)
    
    @staticmethod
    def build_thread_from_dict(dict):
        logging.info(dict.keys())
        logging.info(dict)
        logging.info("""type(dict['offset']:%s"""%str(type(dict['offset'])))
        t=Thread_db(   thread_id=dict['thread_id'],
                    title=xmw.xmw_decoding(dict['title']).decode('utf-8'),
                    #created= dict['created'],
                    lst_page_url=dict['lst_page_url'],
                    page_cnt=dict['page_cnt'],
                    chrAuthor=xmw.xmw_decoding(dict['chrAuthor']).decode('utf-8'),
                    #latest_post_date=dict['latest_post_date'],
                    status=dict['status'],
                    offset=dict['offset'],
                    #sub_count=dict['sub_count']
                    
                )
        if dict['created'] is not None:
            t.created =xmw.ascii_to_datetime(dict['created'].__str__())
        if dict['latest_post_date'] is not None:
            t.latest_post_date=xmw.ascii_to_datetime(dict['latest_post_date'].__str__()) 
            
        return t
    
    def to_thread_dict(self):
        dict={
                'thread_id':self.thread_id,
                'created':self.created,
                'lst_page_url':self.lst_page_url,
                'page_cnt':self.page_cnt,
                'chrAuthor':xmw.xmw_encoding(self.chrAuthor.encode('utf-8')),
                #'chrAuthor':'chrAuthor',
                'latest_post_date':self.latest_post_date,
                'status':self.status,
                'offset':self.offset,
                'title':xmw.xmw_encoding(self.title.encode('utf-8')),
                #'sub_count':self.sub_count,
                
              }
        return dict